<#include "copyRight.ftl">
package ${package.Controller};

import java.util.Arrays;
import java.util.List;

import javax.validation.Valid;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
<#if restControllerStyle>
import org.springframework.web.bind.annotation.RestController;
<#else>
import org.springframework.stereotype.Controller;
</#if>
import org.springframework.ui.Model;
import org.springframework.validation.Errors;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.l.core.util.WebUtils;
import com.l.core.web.RestCode;
import com.l.core.web.RestJson;
import com.l.web.master.video.entity.VideoContent;
import com.l.web.master.video.service.IVideoContentService;
<#if superControllerClassPackage??>
import ${superControllerClassPackage};
</#if>

<#assign service=table.serviceName[1..]?uncap_first>
/**
 * <p>
 * ${table.comment} 前端控制器
 * </p>
 *
 * @author ${author}
 * @since ${date}
 */
<#if restControllerStyle>
@RestController
<#else>
@Controller
</#if>
@RequestMapping("/master<#if package.ModuleName??>/${package.ModuleName}</#if>/<#if controllerMappingHyphenStyle??>${controllerMappingHyphen}<#else>${table.entityPath}</#if>")
<#if kotlin>
class ${table.controllerName}<#if superControllerClass??> : ${superControllerClass}()</#if>
<#else>
<#if superControllerClass??>
public class ${table.controllerName} extends ${superControllerClass} {
<#else>
public class ${table.controllerName} {

    @Autowired
    private ${table.serviceName} ${service};

    @RequestMapping({ "/", "index" })
    public String index(Model model) {
        return "master/${package.ModuleName}/${table.entityPath}/index";
    }

    @RequestMapping("page")
    @ResponseBody
    public RestJson page(${entity} entity) {
        Page<${entity}> page = WebUtils.getPage();
        EntityWrapper<${entity}> wrapper = new EntityWrapper<>();
        wrapper.orderBy("create_time", false);
        page = ${service}.selectPage(page, wrapper);
        return RestJson.Success().page(page);
    }

    @RequestMapping("edit")
    public String edit(Model model, String id) {
        if (StringUtils.isNotEmpty(id)) {
            ${entity} entity = ${service}.selectById(id);
            model.addAttribute("entity", entity);
        }

        return "master/${package.ModuleName}/${table.entityPath}/edit";
    }

    @RequestMapping("save")
    @ResponseBody
    public RestJson save(@Valid ${entity} entity, Errors errors) {
        if (errors.hasErrors()) {
            return new RestJson(RestCode.FORM_BAD, errors);
        }

        if (StringUtils.isEmpty(entity.getId())) {
            ${service}.insert(entity);
        } else {
            ${service}.updateById(entity);
        }

        return RestJson.Success().put("id", entity.getId());
    }

    @RequestMapping("remove")
    @ResponseBody
    public RestJson remove(String[] id) {
        if (id == null || id.length == 0) {
            return new RestJson(RestCode.FAILED.getCode(), "请设置删除id");
        }

        List<String> list = Arrays.asList(id);
        ${service}.deleteBatchIds(list);
        return RestJson.Success();
    }
}
</#if>
</#if>
